<?php
/**
 * 活动投放的卡券
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019-03-28
 * Time: 下午 4:28
 */
namespace App;

use Model\SponsorCouponMod;
use Pub\cache\Cache;

class SponsorCoupon extends Action
{
    private $SponsorCoupon = null;   //活动投放的卡券

    public function __construct()
    {
        parent::__construct();
        $this->SponsorCoupon = new SponsorCouponMod();
    }

    /**
     * 新增卡券
     * @param data 添加数据
     */
    public function add()
    {
        Cache::remove(Cache::ACTIVITY_INFO,array('activity_id'=>$this->_gpc['data']['activity_id']));
        if($this->SponsorCoupon->insert($this->_gpc['data'])){
            $this->_result = array('status'=>'success');
        }
        $this->result($this->_result);
    }

    /**
     * 修改卡券
     * @param coupo_id 操作卡券id
     * @param data 修改的数据
     */
    public function edit()
    {
        Cache::remove(Cache::ACTIVITY_INFO,array('activity_id'=>$this->_gpc['data']['activity_id']));
        $_activity_coupon = $this->SponsorCoupon->findOne(array('id'=>$this->_gpc['coupon_id']),array('name','icon','rule','money'));
        if($_activity_coupon['name'] != $this->_gpc['data']['name'] || $_activity_coupon['icon'] != $this->_gpc['data']['icon'] || $_activity_coupon['rule'] != $this->_gpc['data']['rule'] || $_activity_coupon['money'] != $this->_gpc['data']['money']){
            $this->_gpc['data']['edit_time'] = TIMESTAMP;
        }
        if($this->SponsorCoupon->update($this->_gpc['data'],array('id'=>$this->_gpc['coupon_id']))){
            $this->_result = array('status'=>'success');
        }
        $this->result($this->_result);
    }

    /**
     * admin端获取卡券列表
     * @param activity_id 活动id
     */
    public function getList()
    {
        $_list = $this->SponsorCoupon->findAll(array('activity_id'=>$this->_gpc['activity_id']),array('order'=>array('status','asc','id','desc')),array('id','name','icon','rule','money','number','give_number','user_number','win_odds','use_limit_type','limit_time','status'));
        if(count($_list) > 0){
            foreach($_list as $_k=>$_v){
                if($_v['use_limit_type'] == 2) $_list[$_k]['limit_time_date'] = date('Y-m-d',$_v['limit_time']);
            }
            $this->_result = array('status'=>'success','data'=>$_list);
        }
        $this->result($this->_result);
    }

    /**
     * 检测活动是否投放了卡券
     * @param activity_id 活动id
     */
    public function testPutCoupon()
    {
        $_activity_coupon = $this->SponsorCoupon->findOne(array('activity_id'=>$this->_gpc['activity_id']),array('id'));
        if($_activity_coupon){
            $this->_result = array('status'=>'success');
        }
        $this->result($this->_result);
    }

    /**
     * 修改投放卡券的状态
     */
    public function changeStatus()
    {
        if($this->SponsorCoupon->update(array('status'=>$this->_gpc['status']),array('id'=>$this->_gpc['coupon_id']))){
            Cache::remove(Cache::ACTIVITY_INFO,array('activity_id'=>$this->_gpc['activity_id']));
            $this->_result = array('status'=>'success');
        }
        $this->result($this->_result);
    }

}